package stark.app.function.calculator;

import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.TimeUtils;
import com.chad.library.adapter.base.provider.BaseItemProvider;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.stark.calculator.science.CalRecord;

import stark.app.R;
import stark.common.basic.adaptermutil.StkProviderMultiAdapter;

/**
 * Author：daizhongbin on 2024/11/8
 * Email：1027154338@qq.com
 * Desc:
 */
public class CalRecordAdapter extends StkProviderMultiAdapter<CalRecord> {

    public CalRecordAdapter() {
        super();
        addItemProvider(new MyItemProvider());
    }

    class MyItemProvider extends BaseItemProvider<CalRecord> {

        @Override
        public int getItemViewType() {
            return StkProviderMultiAdapter.TYPE_VIEW;
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_cal_record;
        }

        @Override
        public void convert(@NonNull BaseViewHolder holder, CalRecord calRecord) {
            boolean showTime = false;
            int pos = holder.getBindingAdapterPosition();
            String timeStr = getTimeStr(calRecord.time);
            String today = getTimeStr(System.currentTimeMillis());
            if (pos == 0) {
                showTime = true;
            } else {
                CalRecord pre = getItem(pos - 1);
                String preTimeStr = getTimeStr(pre.time);
                showTime = !timeStr.equals(preTimeStr);
            }
            TextView tvTime = holder.getView(R.id.tvTime);
            tvTime.setVisibility(showTime ? View.VISIBLE : View.GONE);
            if (showTime) {
                if (timeStr.equals(today)) {
                    tvTime.setText("今天");
                } else {
                    tvTime.setText(timeStr);
                }
            }
            holder.setText(R.id.tvExpression, calRecord.expression);
            holder.setText(R.id.tvRet, "=" + calRecord.ret);
        }
    }

    private String getTimeStr(long time) {
        return TimeUtils.millis2String(time, "yyyy-MM-dd");
    }
}
